package com.zsyh.changshang.adress;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;


import com.zsyh.changshang.R;
import com.zsyh.changshang.basic.RetrofitHelper;
import com.zsyh.changshang.bean.PrivicesBean;
import com.zsyh.changshang.data.DataApi;
import com.zsyh.changshang.util.EmptyUtil;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;


/**
 * Created by zzq on 16/9/26.
 */
public class DistrictFragment implements AdapterView.OnItemClickListener{
    private AddressCallBack callBack;
    private String code ;
    private Context context;
    private AddressAdapter adapter;
    private ListView listview;
    private String provinceCode ;
    private String cityCode;
    private List<AddressManager.District> districtsList = new ArrayList<AddressManager.District>();

    private DataApi dataApi;

    public DistrictFragment(Context context, AddressCallBack callBack){
        this.context = context;
        this.callBack = callBack;
        initView();
    }
    public void setCode(String provinceCode, String cityCode, String code){
        Log.e("1502288",cityCode);
        this.cityCode = cityCode;
        this.provinceCode = provinceCode;
        if(districtsList!=null){
            districtsList.clear();
        }
        if(adapter!=null){
            adapter.notifyDataSetChanged();
        }

        getProvince(cityCode);


    }

    public ListView getListview() {
        return listview;
    }

    public View initView() {

        listview = (ListView) LayoutInflater.from(context).inflate(R.layout.select_address_pop_listview,null);

       //getProvince((String) SPUtils.get(MyAppliaction.getContext(), "areashuju", ""));

        listview.setOnItemClickListener(this);

        return listview;
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        code = districtsList.get(i).getCode();
        if (callBack != null){
            callBack.selectDistrict(districtsList.get(i));
        }
        adapter.notifyDataSetChanged();
    }

    class AddressAdapter extends BaseAdapter {

        private List<AddressManager.District> list;

        public AddressAdapter(List<AddressManager.District> list){
            this.list = list;
        }

        @Override
        public int getCount() {
            if (list != null) {
                return list.size();
            }
            return 0;
        }

        @Override
        public Object getItem(int i) {
            return list.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            view = LayoutInflater.from(context).inflate(R.layout.address_listiew_item_textview, null);
            TextView text = (TextView) view.findViewById(R.id.tvTextName);
            ImageView ivSelect = (ImageView) view.findViewById(R.id.ivSelect);
            text.setText(list.get(i).getName());
            if (list.get(i).getCode().equals(code)) {
                text.setTextColor(context.getResources().getColor(R.color.new_redbg));
                ivSelect.setVisibility(View.VISIBLE);
            }
            return view;
        }
    }


    public void getProvince(final String id){
        dataApi = RetrofitHelper.getInstance().getRetrofit().create(DataApi.class);
        dataApi.getCityArea(id)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Observer<PrivicesBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(PrivicesBean privicesBean) {
                        Log.e("areashiqu",privicesBean.toString());
                        if(privicesBean.getErrorCode().equals("0")){
                            if(districtsList!=null){
                                districtsList.clear();
                            }

                            if(EmptyUtil.isEmpty(privicesBean.getData())){

                            }else{
                                for(int i = 0; i<privicesBean.getData().size(); i++){
                                    AddressManager.District city = new AddressManager.District(privicesBean.getData().get(i).getAreaName(), privicesBean.getData().get(i).getId(), "","");
                                    districtsList.add(city);

                                }
                            }
                            adapter = new AddressAdapter(districtsList);
                            listview.setAdapter(adapter);

                        }

                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.e("areashiquerr",e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });

    }
}
